package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Project;
import com.bmp.model.vo.ProjectVoProStatus;
import com.bmp.service.ProStatusManager;
import com.bmp.service.ProjectManager;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private ProjectManager projectManager;
	private ProStatusManager proStatusManager;
	private Project project;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	//初始化projectManager
	public ProjectAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		projectManager = (ProjectManager)ctx.getBean("projectManager");
		proStatusManager = (ProStatusManager)ctx.getBean("proStatusManager");
	}
	
	//显示所有工程信息-JSON格式
	public String projectInfo(){
		//System.out.println("hello projectInfo");
		//System.out.println("rows="+rows);
		//Project p = new Project();
		//ProStatus ps = new ProStatus();
		//ps = this.proStatusManager.getProStatusById(p.getStatus_id());
		//ActionContext.getContext().getSession().put("proStatus", ps);
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;  
        List<ProjectVoProStatus> projectList = projectManager.getProject(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", projectManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", projectList);//rows键 存放每页记录 list  
        //System.out.println("intPage="+intPage);
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        //System.out.println("number="+number);
		return "projectInfo";
	}
	
	//增加项目
	public String addProject(){
		projectManager.addProject(this.project);
		return "success";
	}
	
	//删除项目
	public String deleteProject(){
		projectManager.deleteProjectById(this.project.getId());
		return "success";
	}
	
	//更新项目信息
	public String updateProject(){
		projectManager.updateProject(this.project);
		return "success";
	}
	
	/****************getter and setter******************/
	public ProjectManager getProjectManager() {
		return projectManager;
	}

	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public ProStatusManager getProStatusManager() {
		return proStatusManager;
	}

	public void setProStatusManager(ProStatusManager proStatusManager) {
		this.proStatusManager = proStatusManager;
	}
}
